const Koa = require('koa');
const Router = require('koa-router');
const mongoose = require('mongoose');
const koaBody = require('koa-body');
const app = new Koa();
const router = new Router();
const Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost:27017/bug', {useNewUrlParser: true});
app.use(require('koa-static')('./public'));
app.use(koaBody());

var bugSchema = new Schema({
  goodsName: String,
  bugInfo: String,
  level: String,
  restore: { type: Boolean, default: false }
})

var bug = mongoose.model('bug', bugSchema);

router.get('/bugList', async (ctx, next) => {
  let list = await bug.find()
  ctx.body = {
    code: 1,
    data: list
  }
})

router.post('/addBug', async (ctx) => {
  let addInfo = await bug.create(ctx.request.body)
  ctx.body = {
    code: 1,
    data: addInfo
  }
})

router.post('/deleteBug', async ctx => {
  let deleteBug = await bug.remove(ctx.request.body)
  ctx.body = {
    code: 1,
    data: deleteBug
  }
})

router.post('/updateBug', async ctx => {
  let updateInfo = await bug.updateOne(ctx.request.body, {restore: true})
  ctx.body = {
    code: 1,
    data: updateInfo
  }
})

app
  .use(router.routes())
  .use(router.allowedMethods());
app.listen(3000);